package com.temi.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.temi.demo.activity.ContactsDetailActivity;
import com.temi.demo.activity.PresenterActivity;
import com.temi.demo.adapter.ContactsListAdapter;
import com.temi.demo.anim.EasyTransitionOptions;
import com.temi.demo.bean.ContactsListResult;

import java.util.ArrayList;

import butterknife.BindView;
import utils.FileUtils;

public class MainActivity extends PresenterActivity<ContactsPresenter, MVPCallback>
        implements MVPCallback, SwipeRefreshLayout.OnRefreshListener {

    public static String KEY_CONTACTS_DETAIL = "key_contacts_detail";

    @BindView(R.id.activity_main_swipeRefreshView)
    protected SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.activity_main_recyclerView)
    protected RecyclerView mRecyclerView;

    private ArrayList<ContactsListResult.ContactsData> contactsList;
    private ContactsListAdapter adapter;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    protected void initData() {
        super.initData();
        contactsList = new ArrayList<>();

        getPresenter().getContactsList();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        intiSwipeRefreshLayout();
        initRecyclerView();
    }

    private void intiSwipeRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor("#303F9F"));
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setRefreshing(true);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ContactsListAdapter(this, contactsList);
        mRecyclerView.setAdapter(adapter);
        adapter.setClickListener(new ContactsListAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ContactsListResult.ContactsData contactsBean = contactsList.get(position);
                Bundle bundle = new Bundle();
                bundle.putString(KEY_CONTACTS_DETAIL, new Gson().toJson(contactsBean));

                EasyTransitionOptions options = EasyTransitionOptions.makeTransitionOptions(
                        MainActivity.this,
                        view.findViewById(R.id.user_icon));

                startAction(ContactsDetailActivity.class, bundle, options);
            }
        });
    }

    @Override
    public ContactsPresenter createPresenter() {
        return new ContactsPresenter();
    }

    @Override
    public MVPCallback createView() {
        return this;
    }

    @Override
    public void onSuccess(ArrayList<ContactsListResult.ContactsData> list) {
        mSwipeRefreshLayout.setRefreshing(false);
        contactsList.clear();
        contactsList.addAll(list);
        sortData();
        adapter.setData(contactsList);
    }

    @Override
    public void onFailure() {
        mSwipeRefreshLayout.setRefreshing(false);

        //use local data
        String jsonStr = FileUtils.readJSONFromAsset(this, "contacts.json");
        if (!TextUtils.isEmpty(jsonStr)) {
            Gson gson = new Gson();
            ArrayList<ContactsListResult.ContactsData> list = gson.fromJson(jsonStr,
                    new TypeToken<ArrayList<ContactsListResult.ContactsData>>() {
                    }.getType());

            contactsList.clear();
            contactsList.addAll(list);
            sortData();
            adapter.setData(contactsList);
        }
    }

    /**
     * sort data
     */
    private void sortData() {

    }

    @Override
    public void onRefresh() {
        getPresenter().getContactsList();
    }
}
